
import mongoose from 'mongoose'
import Promise from 'bluebird'
import { mongo } from '../config'
import logger from '../utils/logger'

const version = Number(mongoose.version.match(/(\d+)(\.)(\d+)/)[0])
const options = {
  ...version < 5 ? {
    useMongoClient: true
  } : null
}

mongoose.Promise = Promise
mongoose.connect(mongo.uri, options, err => {
  if (err) {
    logger.error(`connect to ${mongo.uri} error: ${err.message}`)
    process.exit(1)
  }
})

export const uniqueError = e => e.code === 11000

export { default as seqDao } from './seq'
export { default as groupDao } from './group'
export { default as userDao } from './user'
export { default as uploadDao } from './upload'
